//
//  MCTeamChargeViewController.m
//  TLYL
//
//  Created by miaocai on 2017/10/18.
//  Copyright © 2017年 TLYL01. All rights reserved.
//
#import "MCTopSelectedView.h"
#import "MCTeamChargeViewController.h"
#import "UIColor+MCColor.h"
#import "UIImage+Extension.h"
#import "MCMMChargeTableViewCell.h"
#import "MCTCReportSubModel.h"
#import <MJRefresh/MJRefresh.h>
#import "MCRefreshNormalHeader.h"
#import "MCMMIputView.h"
#import "MCTeamCTPopView.h"
#import "DatePickerView.h"
#import "MCNaviButton.h"
#import "MCTCReportSelfModel.h"
#import "MCMMChargeAllTabCell.h"
#import "MCRecordTool.h"
#import "MCBreadcrumbTrailView.h"

@interface MCTeamChargeViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
/**下部滑动根view*/
@property (nonatomic,weak)MCTopSelectedView *topSelectedView;
@property (nonatomic,weak) UIScrollView *baseScrollView;
@property (nonatomic,weak) UIButton *lastBtn;
@property (nonatomic,weak) UIView *topBtnView;
@property (nonatomic,weak) UIView *botView;
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,weak) UITableView *tableViewself;
@property (nonatomic,weak) UITableView *tableViewAll;
@property (nonatomic,strong) MCTCReportSubModel *reportModel;
@property (nonatomic,strong) MCTCReportSelfModel *reportSelfModel;
@property (nonatomic,strong) MCTCReportSelfModel *reportAllModel;

@property (nonatomic,assign) int CurrentPageIndex;
@property (nonatomic,strong) NSDate *minDate;
@property (nonatomic,strong) NSDate *maxDate;
@property (nonatomic,strong) NSDate *beginDate;
@property (nonatomic,strong) NSDate *endDate;
@property (nonatomic,assign) BOOL isShowPopView;
///**条件选择*/
@property (nonatomic,weak) MCMMIputView *viewPop;
///**筛选框*/
@property (nonatomic,  weak) MCTeamCTPopView *popView;
/**数据源tableViewDataArray*/

/**数据源tableViewDataArray*/
@property (nonatomic,strong) NSMutableArray *tableViewSelfDataArray;
/**数据源tableViewDataArray*/
@property (nonatomic,strong) NSMutableArray *tableViewAllDataArray;
/**datePicker 日期选择器*/

/**datePicker 日期选择器*/
@property (nonatomic,  weak) UIButton  *btn;
@property (nonatomic,strong) NSDate *minDateEnd;
@property (nonatomic,strong) NSDate *minDateEndStart;
@property (nonatomic,strong) NSString *stValueLabelStr;
@property (nonatomic,strong) NSString *statusLabDetailStr;
@property (nonatomic,strong) MCBreadcrumbTrailView *trailView;
@property (nonatomic,strong) NSMutableArray *cengJiModelArray;
@property (nonatomic,strong) NSMutableArray *cengJiArray;

@property (nonatomic,assign) BOOL flag1;
@property (nonatomic,assign) BOOL flag2;
@property (nonatomic,assign) BOOL flag3;

@property (nonatomic,assign) NSInteger errCode;
@end

@implementation MCTeamChargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"团队报表";
    
    [self setUpUI];
    [self configRequstNormalParmas];
 
    
}
- (NSMutableArray *)cengJiModelArray{
    if (_cengJiModelArray == nil) {
        _cengJiModelArray = [NSMutableArray array];
        
    }
    return _cengJiModelArray;
}
- (NSMutableArray *)cengJiArray{
    if (_cengJiArray == nil) {
        _cengJiArray = [NSMutableArray array];
        
    }
    return _cengJiArray;
}
-(void)loadListData{
    [self.tableViewDataArray removeAllObjects];
    [self.tableView reloadData];
    [self syncServerData];

}
#pragma mark --添加🔍
- (void)addNavRightBtn {
    UIButton *btn = [[UIButton alloc] init];
    [btn setBackgroundImage:[UIImage imageNamed:@"debss"] forState:UIControlStateNormal];
    self.btn =btn;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn sizeToFit];
    [btn addTarget:self action:@selector(allBtnClick:) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self syncServerData];
    [self addNavRightBtn];
    [[NSUserDefaults standardUserDefaults] setObject:@"MCTeamChargeViewController" forKey:@"MCTeamChargeViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti) name:@"MCTeamChargeViewController" object:nil];
}
- (void)noti{
    [self.cengJiModelArray removeLastObject];
    MCTCReportSubModel *m  = [self.cengJiModelArray lastObject];
    self.userID = [NSString stringWithFormat:@"%d",m.UserID];
    [self.cengJiArray removeLastObject];
    [[NSUserDefaults standardUserDefaults] setObject:self.cengJiArray forKey:@"cengJiArray"];
    [self syncServerData];
    [self.trailView.collection reloadData];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MCTeamChargeViewController"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"cengJiArray"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    
}
- (void)dealloc{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"MCTeamChargeViewController"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"cengJiArray"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
#pragma mark - configRequstNormalParmas
- (void)configRequstNormalParmas{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    self.minDate = [NSDate date];
    self.maxDate = [NSDate date];
    if (self.userName == nil || [self.userName isEqualToString:@""]) {
        self.userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    }
    
    if (self.userID == nil || [self.userID isEqualToString:@""]) {
        self.userID=[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    }
    if (self.GetUserType == 0) {
        self.GetUserType = 0;
    }
    
    self.CurrentPageIndex = 1;
    self.minDateEnd = self.minDate;
    self.minDateEndStart = self.minDate;
    if (self.beginDate||self.endDate) {
        NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
        [dataFormatter setDateFormat:@"yyyy/MM/dd"];
        self.popView.endDateLabDetail.text =  [dataFormatter stringFromDate:self.endDate];
        self.popView.startDateLabDetail.text =  [dataFormatter stringFromDate:self.beginDate];
    } else {
        self.beginDate = self.minDate;
        self.endDate = self.maxDate;
    }
    if ([self.stValueLabelStr isEqualToString:@""] || self.stValueLabelStr==nil) {
        self.popView.stValueLabel.text = @"当天记录";
    } else {
        self.popView.stValueLabel.text = self.stValueLabelStr;
    }
    if ([self.statusLabDetailStr isEqualToString:@""] || self.statusLabDetailStr==nil) {
        self.popView.statusLabDetail.text = @"全部";
    } else {
        self.popView.statusLabDetail.text = self.statusLabDetailStr;
    }
    
    
}
- (void)syncServerData{
    [BKIndicationView showInView:self.view];
    WS(weakself);
    // 创建信号量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    // 创建全局并行
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        [weakself loadListData: semaphore];
        
    });
    
    dispatch_group_async(group, queue, ^{
        [weakself loadSelfListData: semaphore];
        
    });
    
    dispatch_group_async(group, queue, ^{
         [weakself loadAllListData: semaphore];
    });

    dispatch_group_notify(group, queue, ^{
        
        // 三个请求对应三次信号等待
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
       
        dispatch_async(dispatch_get_main_queue(), ^{
            [BKIndicationView dismiss];
            if (weakself.flag1 == YES ||weakself.flag2 == YES ||weakself.flag3 == YES ) {
                [weakself showExcaptionViewWithErrCode:weakself.errCode];
            }
            if (weakself.flag1 == NO &&weakself.flag2 == NO &&weakself.flag3 == NO ) {
                [weakself hideExceptionView];
              
                [weakself.tableView reloadData];
            }

        });
    });
}
-(void)setUpUI{
    [self addNavRightBtn];
    self.automaticallyAdjustsScrollViewInsets = NO;
    MCBreadcrumbTrailView *trailView = [[MCBreadcrumbTrailView alloc] initWithFrame:CGRectMake(0,kStatusBarAndNavigationBarHeight, G_SCREENWIDTH, MC_REALVALUE(30))];
    trailView.backgroundColor = RGB(249, 249, 249);
    self.trailView = trailView;
    NSString *fName = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    [self.cengJiArray addObject:fName];
    MCTCReportSubModel *item = [[MCTCReportSubModel alloc] init];
    item.UserName = fName;
    item.UserID = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];

    [self.cengJiModelArray addObject:item];
    trailView.dataArr = self.cengJiArray;
    trailView.selectedBreadcrumbTrailBlock = ^(NSInteger i) {
        [self.cengJiModelArray removeObjectsInRange:NSMakeRange((i + 1) , self.cengJiArray.count - i -1)];
        [self.cengJiArray removeObjectsInRange:NSMakeRange((i + 1) , self.cengJiArray.count - i -1)];
        MCTCReportSubModel *m  = [self.cengJiModelArray objectAtIndex:i];
        self.userID = [NSString stringWithFormat:@"%d",m.UserID];
        
        self.userName = m.UserName;
        
        [self syncServerData];
        [self.trailView.collection reloadData];
    };
    [self.view addSubview:trailView];
    UITableView *tab = [[UITableView alloc] initWithFrame:CGRectMake(0, MC_REALVALUE(30) + kStatusBarAndNavigationBarHeight, G_SCREENWIDTH, G_SCREENHEIGHT - MC_REALVALUE(40) - kStatusBarAndNavigationBarHeight - kTabBarHeight) style:UITableViewStylePlain];
    [self.view addSubview:tab];
    self.tableView = tab;
    __weak typeof(self) weakself = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_header = [MCRefreshNormalHeader headerWithRefreshingBlock:^{

        [weakself syncServerData];
    }];
    tab.rowHeight = MC_REALVALUE(215);
    [tab registerClass:[MCMMChargeTableViewCell class] forCellReuseIdentifier:@"cell"];
    [tab registerClass:[MCMMChargeAllTabCell class] forCellReuseIdentifier:@"cellself"];
    [tab registerClass:[MCMMChargeAllTabCell class] forCellReuseIdentifier:@"cellall"];
    tab.delegate = self;
    tab.dataSource = self;
    tab.backgroundColor = RGB(231, 231, 231);
    self.view.backgroundColor = RGB(231, 231, 231);
  
}

#pragma mark --发送请求
-(void)loadListData:(dispatch_semaphore_t)semaphore{
    self.tableView.mj_footer.hidden=NO;
    self.CurrentPageIndex = 1;
    __weak typeof(self) weakself = self;
    MCTCReportSubModel *reportModel = [[MCTCReportSubModel alloc] init];
    self.reportModel = reportModel;
    reportModel.User_Name = self.userName;
    reportModel.User_ID = self.userID;
    reportModel.RebateType = 1;
    reportModel.GetUserType = self.GetUserType;
    reportModel.CurrentPageIndex = 1;
    reportModel.CurrentPageSize = 15;
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
    [dataFormatter setDateFormat:@"yyyy/MM/dd"];
    NSString *dateTemps = [dataFormatter stringFromDate:self.beginDate];
    NSString *dateTempe = [dataFormatter stringFromDate:self.endDate];
    reportModel.BeginTime = [NSString stringWithFormat:@"%@ 00:00:00",dateTemps];
    reportModel.EndTime = [NSString stringWithFormat:@"%@ 23:59:59",dateTempe];
    [reportModel refreashDataAndShow];
   
    reportModel.callBackSuccessBlock = ^(id manager) {
        dispatch_semaphore_signal(semaphore);
        [weakself tableViewEndRefreshing];
        weakself.flag1 = NO;
        weakself.tableViewDataArray = [MCTCReportSubModel mj_objectArrayWithKeyValuesArray:manager[@"ReportComm"]];

        if (weakself.tableViewDataArray.count>=[manager[@"DataCount"] integerValue]) {
            weakself.tableView.mj_footer.hidden=YES;
        }
        
    };
    reportModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSDictionary *errorCode) {
        dispatch_semaphore_signal(semaphore);
        [weakself tableViewEndRefreshing];
        weakself.flag1 = YES;
        if ([errorCode[@"code"] isKindOfClass:[NSError class]]) {
            NSError *err = errorCode[@"code"];
            weakself.errCode = err.code;

        }
    };
    
}
- (void)loadMoreData{
    
    self.tableView.mj_footer.hidden=NO;
    self.reportModel.CurrentPageIndex = ++self.CurrentPageIndex;
    __weak typeof(self) weakSelf = self;
    self.reportModel.callBackSuccessBlock = ^(id manager) {
       
        [weakSelf tableViewEndRefreshing];
        NSArray *arr = [MCTCReportSubModel mj_objectArrayWithKeyValuesArray:manager[@"ReportComm"]];
        [weakSelf.tableViewDataArray addObjectsFromArray:arr];
        [weakSelf.tableView reloadData];
        [weakSelf tableViewEndRefreshing];
        if (weakSelf.tableViewDataArray.count >=[manager[@"DataCount"] integerValue]) {
            weakSelf.tableView.mj_footer.hidden=YES;
        }
    };
    self.reportModel.callBackFailedBlock = ^(id manager, NSDictionary *errorCode) {
       
        [weakSelf tableViewEndRefreshing];
    };
    
    [self.reportModel refreashDataAndShow];
}
- (void)loadSelfListData:(dispatch_semaphore_t)semaphore{
    __weak typeof(self) weakself = self;
    [self.tableViewSelfDataArray removeAllObjects];
    MCTCReportSelfModel *reportSelfModel = [[MCTCReportSelfModel alloc] init];
    reportSelfModel.allTeam = YES;
    reportSelfModel.User_Name = self.userName;
    self.reportSelfModel = reportSelfModel;
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
    [dataFormatter setDateFormat:@"yyyy/MM/dd"];
    NSString *dateTemps = [dataFormatter stringFromDate:self.beginDate];
    NSString *dateTempe = [dataFormatter stringFromDate:self.endDate];
    reportSelfModel.BeginTime = [NSString stringWithFormat:@"%@ 00:00:00",dateTemps];
    reportSelfModel.EndTime = [NSString stringWithFormat:@"%@ 23:59:59",dateTempe];
    [reportSelfModel refreashDataAndShow];
    
    reportSelfModel.callBackSuccessBlock = ^(id manager) {
        dispatch_semaphore_signal(semaphore);
        [weakself tableViewEndRefreshing];
        weakself.flag2 = NO;
        MCTCReportSelfModel *reportSelfModel = [MCTCReportSelfModel mj_objectWithKeyValues:manager];
        if (reportSelfModel != nil) {
            [weakself.tableViewSelfDataArray addObject:reportSelfModel];
        }
    };
    reportSelfModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSDictionary *errorCode) {
        dispatch_semaphore_signal(semaphore);
        [weakself tableViewEndRefreshing];
         weakself.flag2 = YES;
    };
}

- (void)loadAllListData:(dispatch_semaphore_t)semaphore{
    __weak typeof(self) weakself = self;
    [self.tableViewAllDataArray removeAllObjects];
    MCTCReportSelfModel *reportSelfModel = [[MCTCReportSelfModel alloc] init];
    reportSelfModel.allTeam = NO;
    reportSelfModel.User_Name = self.userName;
    self.reportAllModel = reportSelfModel;
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
    [dataFormatter setDateFormat:@"yyyy/MM/dd"];
    NSString *dateTemps = [dataFormatter stringFromDate:self.beginDate];
    NSString *dateTempe = [dataFormatter stringFromDate:self.endDate];
    reportSelfModel.BeginTime = [NSString stringWithFormat:@"%@ 00:00:00",dateTemps];
    reportSelfModel.EndTime = [NSString stringWithFormat:@"%@ 23:59:59",dateTempe];
    [reportSelfModel refreashDataAndShow];
   
    reportSelfModel.callBackSuccessBlock = ^(id manager) {
        weakself.flag3 = NO;
        dispatch_semaphore_signal(semaphore);
        [weakself tableViewEndRefreshing];
        
        MCTCReportSelfModel *model= [MCTCReportSelfModel mj_objectWithKeyValues:manager];
        if (model != nil) {
            [weakself.tableViewAllDataArray addObject:model];
        }
    };
    reportSelfModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSDictionary *errorCode) {
        dispatch_semaphore_signal(semaphore);
        weakself.flag3 = YES;
        [weakself tableViewEndRefreshing];

    };
}


- (void)tableViewEndRefreshing{
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (self.tableViewAllDataArray.count>0&&self.tableViewSelfDataArray.count>0) {
        return self.tableViewDataArray.count + 2;
    }
    return 0;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    if (indexPath.row >= 2) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        MCMMChargeTableViewCell *cellT = (MCMMChargeTableViewCell *)cell;
        if (self.tableViewDataArray.count>0) {
            if (self.tableViewDataArray.count >= indexPath.row -2 ) {
                cellT.dataSource = self.tableViewDataArray[indexPath.row - 2];
            }
            
        }
        
    } else if (indexPath.row == 1){
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellself"];
        MCMMChargeAllTabCell *cellT = (MCMMChargeAllTabCell *)cell;
        if (self.tableViewSelfDataArray.count>0) {
            cellT.selfUserName = self.userName;
            cellT.dataSource = self.tableViewSelfDataArray[0];

        }
    }else if (indexPath.row == 0){
        cell = [tableView dequeueReusableCellWithIdentifier:@"cellall"];
        MCMMChargeAllTabCell *cellT = (MCMMChargeAllTabCell *)cell;
        if (self.tableViewAllDataArray.count>0) {
             cellT.dataSourceAll = self.tableViewAllDataArray[0];

        }
       
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row>=2) {
        MCTCReportSubModel *model = self.tableViewDataArray[indexPath.row - 2];
        NSString *str = ((model.Category & 64) == 64) ? @"会员" : @"代理";
      
       if([str isEqualToString:@"会员"] == YES){
            [SVProgressHUD showInfoWithStatus:@"会员没有下级！"];
        }
        else if (model.ChildNum <= 0) {
            [SVProgressHUD showInfoWithStatus:@"该成员没有下级！"];
        } else {
            self.userID = [NSString stringWithFormat:@"%d",model.UserID];
            self.userName = model.UserName;
            
            [self.cengJiArray addObject:model.UserName];
            [self.cengJiModelArray addObject:model];
            [self.trailView.collection reloadData];
            
            [self syncServerData];
        }
        

  
    }
    
}
- (void)allBtnClick:(UIButton *)btn{
    if (_isShowPopView) {
        _isShowPopView=NO;
        [self.popView hidePopView];
    }else{
        [self.view bringSubviewToFront:self.popView];
        _isShowPopView=YES;
        
        typeof(self) weakself = self;
        self.popView.lotteryBlock = ^{
            if (weakself.datePicker) {
                [weakself.datePicker removeFromSuperview];
            }
            weakself.viewPop.dataArray = @[@"当天记录",@"历史记录"];
            weakself.viewPop.cellDidSelected = ^(NSInteger i) {
                
                weakself.popView.stValueLabel.text = weakself.viewPop.dataArray[i];
                weakself.stValueLabelStr = weakself.viewPop.dataArray[i];
                
                [weakself.viewPop hiden];
                [weakself recordBtnClick:i];
            };
            [weakself.viewPop show];
        };
        self.popView.searchBtnBlock = ^(NSString *str) {
            [weakself.datePicker removeFromSuperview];
            self.userName = str;
            _isShowPopView=NO;
            
            NSString *fName = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
            weakself.cengJiArray =[NSMutableArray arrayWithArray:@[fName]] ;
            MCTCReportSubModel *item = [[MCTCReportSubModel alloc] init];
            item.UserName = fName;
            item.UserID = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
            [self.cengJiModelArray addObject:item];
            weakself.trailView.dataArr = weakself.cengJiArray;
            weakself.cengJiModelArray =[NSMutableArray arrayWithArray:@[item]] ;
            weakself.userID = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
            [weakself.trailView.collection reloadData];
            [weakself.viewPop hiden];
            [weakself syncServerData];
           
        };
        self.popView.statusBlock = ^{
            [weakself.datePicker removeFromSuperview];
            weakself.viewPop.dataArray = @[@"全部",@"会员",@"代理"];
            [weakself.viewPop show];
            weakself.viewPop.cellDidSelectedTop = ^(NSInteger i) {
                [weakself.viewPop hiden];
                weakself.popView.statusLabDetail.text = weakself.viewPop.dataArray[i];
                weakself.statusLabDetailStr = weakself.viewPop.dataArray[i];
                self.GetUserType = (int)i;
            };
        };
        [weakself datePickerCallBack];
        [self.popView showPopView];
    }
}
- (void)datePickerCallBack{
    typeof(self) weakself = self;
    self.popView.startDateBlock = ^{
        [weakself.viewPop hiden];
        weakself.minDate = weakself.minDateEnd;
        weakself.datePicker.showDate = weakself.beginDate;
        [weakself cancelDatePicker];
        weakself.datePicker.sureBlock = ^(NSString *selectDateStr) {
            weakself.popView.startDateLabDetail.text = selectDateStr;
            NSDateFormatter *dataFormatter = [weakself dateFormat];
            weakself.minDate = [dataFormatter dateFromString:selectDateStr];
            weakself.beginDate = [dataFormatter dateFromString:selectDateStr];
            weakself.minDateEndStart = weakself.minDate;
            [weakself cancelDatePickerAnimate];
        };
        
    };
    
    self.popView.endDateBlock = ^{
        [weakself.viewPop hiden];
        weakself.minDate = weakself.minDateEndStart;
        weakself.datePicker.showDate = weakself.endDate;
       [weakself cancelDatePicker];
        weakself.datePicker.sureBlock = ^(NSString *selectDateStr) {
            weakself.popView.endDateLabDetail.text = selectDateStr;
            NSDateFormatter *dataFormatter = [weakself dateFormat];
            weakself.endDate = [dataFormatter dateFromString:selectDateStr];
            [weakself cancelDatePickerAnimate];
        };
        
    };
    
}

- (void)tap{
    self.isShowPopView=NO;
    self.popView.hidden = YES;
    [self.viewPop hiden];
}

- (NSMutableArray *)tableViewSelfDataArray{
    
    if (_tableViewSelfDataArray == nil) {
        _tableViewSelfDataArray = [NSMutableArray array];
    }
    return _tableViewSelfDataArray;
}
- (NSMutableArray *)tableViewAllDataArray{
    
    if (_tableViewAllDataArray == nil) {
        _tableViewAllDataArray = [NSMutableArray array];
    }
    return _tableViewAllDataArray;
}

#pragma mark - touch event
- (void)recordBtnClick:(NSInteger)i{
    [self.viewPop hiden];
    if (i == 0) {
        self.minDate = [NSDate date];
        self.maxDate = [NSDate date];
    } else {
        
        self.minDate = [MCRecordTool getLaterDateFromDate:[NSDate date] withYear:0 month:-3 day:1];;
        self.maxDate = [NSDate dateWithTimeIntervalSinceNow:- 1 * 3600 * 24];
    }
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
    [dataFormatter setDateFormat:@"yyyy/MM/dd"];
    self.beginDate = self.maxDate;
    self.endDate = self.maxDate;
    self.popView.startDateLabDetail.text = [dataFormatter stringFromDate:self.beginDate];
    self.popView.endDateLabDetail.text = [dataFormatter stringFromDate:self.endDate];
    self.minDateEnd = self.minDate;
    self.minDateEndStart = self.minDate;
    
}



- (MCTeamCTPopView *)popView{
    
    if (_popView == nil) {
        MCTeamCTPopView * popView = [[MCTeamCTPopView alloc] init];
        [self.view addSubview:popView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [popView addGestureRecognizer:tap];
        _popView = popView;
    }
    
    return _popView;
}

- (MCMMIputView *)viewPop{
    if (_viewPop == nil) {
        
        MCMMIputView *viewStatus = [[MCMMIputView alloc] init];
        [self.navigationController.view addSubview:viewStatus];
        _viewPop = viewStatus;
    }
    return _viewPop;
}




@end
